package NetWork.Exercise004;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

/**
 * ClassName:Server
 * Package:NetWork.Exercise001
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-22:41
 * @Version:v1.0
 */
public class Server {
    public static void main(String[] args) throws IOException {

        //创建对象并连接端口
        ServerSocket serverSocket = new ServerSocket(1000);
        //等待客户端来连接
        Socket socket = serverSocket.accept();

        //读取数据并保存到本地文件中
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        String name = UUID.randomUUID().toString().replace("-", "");
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\IDER\\IDEAWorkCode\\IDEADemo\\src\\NetWork\\Exercise003\\serverdir\\"+name+".jpg"));

        int len;
        byte[] bytes = new byte[1024*1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }

        //回写数据
        String str = "接收完毕";
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(str);
        bw.newLine();
        bw.flush();

        //释放资源
        socket.close();
        serverSocket.close();


    }
}
